﻿using Microsoft.Graphics.Canvas;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using 纸片人.AttributeUsageFloder;
using 纸片人.InfoDataFolder;

// https://go.microsoft.com/fwlink/?LinkId=234238 上介绍了“空白页”项模板

namespace 纸片人.PageViews
{
    [LastModified("2022-5-12", "create Wave.xaml")]
    [PageType("cpu", "波浪效果")]
    /// <summary>
    /// 可用于自身或导航至 Frame 内部的空白页。
    /// </summary>
    public sealed partial class Wave : Page
    {
        LayerBitmapData bitmapData = new LayerBitmapData();

        public Wave()
        {
            this.InitializeComponent();
            Init();
        }

        private void Init()
        {
            selectPicture.Click += async (s, e) => { await bitmapData.SelectOneBitmapAsync(); canvas.Invalidate(); };
            canvas.Draw += (s, e) =>
            {
                Function.DrawGridGraphics(e.DrawingSession);

                if (bitmapData.oneBitmap != null)
                {
                    var element = (FrameworkElement)s;
                    float effectWidth = (float)element.ActualWidth;
                    float effectHeight = (float)element.ActualHeight * 0.7f;
                    float previewWidth = effectWidth;
                    float previewHeight = (float)element.ActualHeight * 0.3f;

                    var previewTran = Function.CalcutateImageCenteredTransform(previewWidth, previewHeight, bitmapData.oneWidth, bitmapData.oneHeight);
                    previewTran.Source = bitmapData.oneBitmap;
                    e.DrawingSession.DrawImage(previewTran, 0, effectHeight);

                    var resultColors = PictureFilter.WaveEffect(bitmapData.oneColors, bitmapData.oneWidth, bitmapData.oneHeight,
                        horizontalWaveLenght.Value, verticallWaveLenght.Value, horizontalAmplitude.Value, verticallAmplitude.Value);
                    var effectBitmap = CanvasBitmap.CreateFromColors(canvas, resultColors, bitmapData.oneWidth, bitmapData.oneHeight);
                    var effectTran = Function.CalcutateImageCenteredTransform(effectWidth, effectHeight, bitmapData.oneWidth, effectHeight);
                    effectTran.Source = effectBitmap;
                    e.DrawingSession.DrawImage(effectTran);
                }
            };

            RangeBaseValueChangedEventHandler ValueChanged = (sender, e) =>
             {
                 canvas.Invalidate();
             };

            horizontalAmplitude.ValueChanged += ValueChanged;
            horizontalWaveLenght.ValueChanged += ValueChanged;
            verticallAmplitude.ValueChanged += ValueChanged;
            verticallWaveLenght.ValueChanged += ValueChanged;

            wave.Click += (s, e) => { canvas.Invalidate(); };
        }
    }
}
